/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package esclavo;

import interfaz_esclavo.Informacion;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author leonardo
 */
public class MainServidor {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        try {
            Registry registry = LocateRegistry.createRegistry(1099);
            registry.rebind("esclavo", new Informacion());
            System.out.println("Servidor Iniciado");
            enviarMensajeBroadcast();
            
            } catch (Exception e) {
              System.out.println(e);

            }
       
        
    }
    
    public static void enviarMensajeBroadcast()
    {
        try {
            DatagramSocket conector = new DatagramSocket(6972,InetAddress.getByName("0.0.0.0"));
            conector.setBroadcast(true);
            while(true){
                byte[] recibido= new byte[20000];
                DatagramPacket paqueteR = new DatagramPacket(recibido, recibido.length);
                conector.receive(paqueteR);
                System.out.println("Ip  emisor: "+ paqueteR.getAddress().getHostAddress());
                System.out.println("Ip emisor: "+ new String (paqueteR.getData()));
                byte[] enviado = "esclavo".getBytes();
                DatagramPacket paqueteE = new DatagramPacket(enviado, enviado.length, InetAddress.getByName(paqueteR.getAddress().getHostAddress()),6972);
                conector.send(paqueteE);
            }
                
        } catch (Exception ex) {
            System.out.println("Error en el Servidor");
            Logger.getLogger(MainServidor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
